/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package supermarketv1;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import supermarketv0_class.*;

/**
 *
 * @author ΠΑΝΑΓΙΩΤΗΣ v1
 */
public class Checkout extends javax.swing.JFrame {

    SuperMarketMainView1 mainscreen;
    Customer customer;
    List numofchecks;
    Store store;
    ArrayList<Product> selectedpro;
    ArrayList selestedquantity;
    ArrayList bank;
    ArrayList checks;
    DefaultListModel categModel;
    CreditCardAuthority cca;
    int counter = 0;
    int total;
    int i = 0;
   boolean delivery;
    /**
     * Creates new form Checkout
     */
    public Checkout(SuperMarketMainView1 mainscreen, Customer customer, ArrayList<Product> selectedpro,
            ArrayList selestedquantity, ArrayList ch, int total, Store store, boolean delivery) {
        initComponents();
        this.mainscreen = mainscreen;
        this.customer = customer;
        this.selectedpro = selectedpro;
        this.selestedquantity = selestedquantity;
        this.total = total;
        this.checks = ch;
        this.store = store;
        this.delivery=delivery;

        
       numofchecks=mainscreen.db.readNumOfCreditOK(customer);
        
       bank = mainscreen.apk.readBanks();//ανάγνωση τραπεζών
        jList1.removeAll();//ΑΔΙΑΣΜΑ ΛΙΣΤΑΣ
        categModel = new DefaultListModel();//ΝΕΟ ΜΟΝΤΕΛΟ
        Iterator itr = bank.iterator();
        while (itr.hasNext()) {//ΔΙΑΤΡΕΧΩ ΤΗ ΛΙΣΤΑ
            Bank ba = (Bank) itr.next();
            categModel.addElement(ba.getS());
        }
        jList1.setModel(categModel);//ΚΟΥΜΠΩΝΩ ΤΟ ΜΟΝΤΕΛΟ ΣΤΗ ΛΙΣΤΑ





    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.setName("jList1"); // NOI18N
        jScrollPane1.setViewportView(jList1);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(supermarketv1.SuperMarketv1App.class).getContext().getResourceMap(Checkout.class);
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N

        jTextField2.setText(resourceMap.getString("jTextField2.text")); // NOI18N
        jTextField2.setName("jTextField2"); // NOI18N

        jTextField3.setText(resourceMap.getString("jTextField3.text")); // NOI18N
        jTextField3.setName("jTextField3"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(46, 46, 46)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextField2)
                                    .addComponent(jTextField3)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)))))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(198, 198, 198)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))))
                .addGap(48, 48, 48)
                .addComponent(jButton1)
                .addContainerGap(65, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:Πληρωμή 
//pistopoihsh kartas
    int cv = Integer.parseInt(jTextField3.getText());
     //  int cvb = Integer.parseInt(cv);
       int cca =  (Integer) mainscreen.apk.getRecord(cv);//ανάγνωση κάρτας

        
        if (cca>0) {//Έγκυρη κάρτα

            Iterator itr = checks.iterator();
            while (itr.hasNext()) {
                Checks cho = (Checks) itr.next();
                total = -cho.getAmount();//ενημέρωση συνόλου πληρωμής
                mainscreen.db.ModifyCheck(cho);//ενημέρωση επιταγών
            }

            Iterator itsp = selectedpro.iterator();
            while (itsp.hasNext()) {
                Product product = (Product) itsp.next();//για κάθε προϊόν 

                int quantity = (Integer) selestedquantity.get(i);//πάρε την ποσότητά του
               
                System.out.println("ΠΟΝΤΟΙ");//υλοποίηση  αγοράς
                List f =mainscreen.db.readPurchases();
                mainscreen.db.makePurchase(customer, store, product, quantity,f,delivery );
                  
            }
         //   int sum = customer.getAvailablePoints();
            // System.out.println(sum);
             
            if (total > 200 ) {//νέες επιταγές
                mainscreen.db.newCheck(customer, mainscreen.db.readChecks());//νέαεπιταγή 
                System.out.println(total);
            }



            JOptionPane.showMessageDialog(null, "Η πληρωμή Έγινε με επιτυχία!  ", "Μessage", JOptionPane.WARNING_MESSAGE);
            dispose();
        } else  {
            counter++;
            JOptionPane.showMessageDialog(null, "Λαθος αριθμός! ", "Error", JOptionPane.WARNING_MESSAGE);
            if (counter == 3) {//akyrosh agoras
                JOptionPane.showMessageDialog(null, "Ακύρωση συναλλαγής", "Error", JOptionPane.WARNING_MESSAGE);
               //αδιασμα επιλογών 
                selectedpro.clear();
                selestedquantity.clear();
                checks.clear();
                counter = 0;
                
                mainscreen.shopcart.dispose();
                
                dispose();

            }
            
        }
    

    }//GEN-LAST:event_jButton1ActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables
}
